﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;

namespace Jalal.Library.Utility
{
    public class CommonUtility
    {
        public static object FilterNull(object value, Type type)
        {
            if (value == DBNull.Value)
            {
                if (type == typeof(int))
                    return 0;
                else if (type == typeof(double))
                    return 0.0;
                else if (type == typeof(string))
                    return string.Empty;
                else if (type == typeof(DateTime))
                    return SqlDateTime.MinValue.Value;
                else if (type == typeof(decimal))
                    return 0;
                else if (type == typeof(bool))
                    return false;
                else
                    return value;
            }
            else
                return value;
        }

        public static string FormatedDateString(DateTime dateTime)
        {
            if (dateTime == SqlDateTime.MinValue.Value)
                return string.Empty;
            else
                return dateTime.ToString("dd/MM/yyyy ");
        }

        public static string FormatedDateTimeString(DateTime dateTime)
        {
            if (dateTime == SqlDateTime.MinValue.Value)
                return string.Empty;
            else
                return dateTime.ToString("dd/MM/yyyy hh:mm");
        } 

        public static string MakeAlphaNumeric(string text)
        {
            StringBuilder filteredText = new StringBuilder();
            StringBuilder newText = new StringBuilder(text);
            for (int i = 0; i < newText.Length; i++)
            {
                if (char.IsLetterOrDigit(newText[i]))
                    filteredText = filteredText.Append(newText[i]);
            }
            return filteredText.ToString();
        }

        public static String MakeCSV(IList<Int32> items)
        {
            StringBuilder result = new StringBuilder(String.Empty);

            for (int i = 0; i < items.Count; i++)
            {
                if (i != 0)
                    result = result.Append(",").Append(items[i]);
                else
                    result = result.Append(items[i]);
            }

            return result.ToString();
        }

        public static String MakeCSV(IList<String> items)
        {
            StringBuilder result = new StringBuilder(String.Empty);

            for (int i = 0; i < items.Count; i++)
            {
                if (i != 0)
                    result = result.Append(",").Append(items[i]);
                else
                    result = result.Append(items[i]);
            }

            return result.ToString();
        }

        public static IList<String> SplitCSV(String csv)
        {
            List<String> result = new List<String>();
            String[] parts = csv.Split(',');

            foreach (String part in parts)
            {
                result.Add(part);
            }

            return result;
        }

        public static IList<Int32> SplitCSVToInt(String csv)
        {
            List<Int32> result = new List<Int32>();
            String[] parts = csv.Split(',');

            foreach (String part in parts)
            {
                result.Add(Int32.Parse(part));
            }

            return result;
        }

        public static string ShortenText(string text, int maxLength)
        {
            StringBuilder newText = new StringBuilder();
            if (text.Length > maxLength)
            {
                newText.Append(text.Substring(0, maxLength - 3));
                newText.Append("...");
                return newText.ToString();
            }
            else
                return text;
        }

    }
}
